package com.xxl.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author xxl
 * @date 2023/3/13
 */
@Component
public class AuthenticationInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        System.out.println("拦截器被执行");
        Object userInfo = session.getAttribute("userInfo");
        if (userInfo != null) {
            return true;
        }
        System.out.println("真实路径="+request.getContextPath());
        //转发到登录
        request.getRequestDispatcher("/view/login.html").forward(request,response);
        return false;
    }
}
